package pe.com.nextel.util;

import java.util.Calendar;
import java.util.Date;
/**
 * Clase de administrar la fecha
 * @author eespinoza
 * @version 1.1, 02/10/07
 * @see Date
 * @see Calendar
 * @since 1.1
 */
public class Fecha {
    /**
     *
     * @return date
     */
    public static Date getDate(){
        return Calendar.getInstance().getTime( );
    }
    
    
    public static String getFechaActual(){
        return convertTimeLongToString(Calendar.getInstance().getTime().getTime())[0];
    }
    
    public static String getHoraActual(){
        return convertTimeLongToString(Calendar.getInstance().getTime().getTime())[1];
    }
    
    public static String getFechaActualCompleta(){
        String[] fecha = convertTimeSSLongToString(Calendar.getInstance().getTime().getTime());
        return fecha[0] + " " + fecha[1];
    }
    
    
    public static Date getDateSiguiente(){
        Calendar calendar = Calendar.getInstance();
        
        long DAY = 24*60*60*1000;
        
        Date date = calendar.getTime();
        long lDate = date.getTime();
        lDate += (DAY * 1);     // date 1 day from current date.
        date.setTime(lDate);
        return date;
    }
     
    public static Date getDateDDMMYYYY(String date){
        
        Calendar calendar = Calendar.getInstance();
        int dia = Integer.parseInt(date.substring(0,2));
        int mes = Integer.parseInt(date.substring(3,5)) -1;
        int anio = Integer.parseInt(date.substring(6));
        
        
        calendar.set(Calendar.YEAR, anio);
        calendar.set(Calendar.MONTH,mes);
        calendar.set(Calendar.DAY_OF_MONTH, dia);
        
        return calendar.getTime();
    }
     
    
    /*
     *COMPARA UNICAMENTE LAS FECHAS
     *
     *DEVUELVE:
     * 0 - LAS FECHAS SON IGUALES
     * 1 - LA FECHA2 ES MAYOR QUE LA FECHA 1
     *-1 - LA FECHA 1 ES MAYOR QUE LA FECHA 2
     */
    public static int compararCalendarFecha(Date fecha1,Date fecha2){
        int rslt = 1;
        Calendar cal1=Calendar.getInstance();
        Calendar cal2=Calendar.getInstance();
        if(fecha1 != null && fecha2 != null){
            cal1.setTime(fecha1);
            cal2.setTime(fecha2);
            if(cal1.get(Calendar.YEAR)>cal2.get(Calendar.YEAR))
                rslt = -1;
            if(cal1.get(Calendar.YEAR)==cal2.get(Calendar.YEAR)){
                if(cal1.get(Calendar.MONTH)>cal2.get(Calendar.MONTH)){
                    rslt = -1;
                }
                if(cal1.get(Calendar.MONTH)==cal2.get(Calendar.MONTH)){
                    if(cal1.get(Calendar.DAY_OF_MONTH)>cal2.get(Calendar.DAY_OF_MONTH)){
                        rslt = -1;
                    }
                    if(cal1.get(Calendar.DAY_OF_MONTH)==cal2.get(Calendar.DAY_OF_MONTH)){
                        rslt = 0;
                    }
                }
            }
        }
        return rslt;
    }
    
    public static boolean esMayorFechaActual(String fecha){
        Date date = getDateDDMMYYYY(fecha);
        int  i = compararCalendarFecha( date, getDate());
        
        if( i == -1 ) return true;
        
        return false;
        
    }
    
    
    
    /*
     *COMPARA UNICAMENTE LAS HORAS
     *
     *DEVUELVE:
     * 0 - LA HORA1 ES MAYOR QUE LA HORA2
     * 1 - LA HORA2 ES MAYOR QUE LA HORA1
     */
    public static int compararCalendarHora(Date fecha1,Date fecha2){
        int rslt = 1;
        Calendar cal1=Calendar.getInstance();
        Calendar cal2=Calendar.getInstance();
        if(fecha1 != null && fecha2 != null){
            cal1.setTime(fecha1);
            cal2.setTime(fecha2);
            if(cal1.get(Calendar.HOUR_OF_DAY)>cal2.get(Calendar.HOUR_OF_DAY))
                rslt = 0;
            if(cal1.get(Calendar.HOUR_OF_DAY)==cal2.get(Calendar.HOUR_OF_DAY)){
                if(cal1.get(Calendar.MINUTE)>cal2.get(Calendar.MINUTE)){
                    rslt = 0;
                }
            }
        }
        return rslt;
    }
    
    public static String formatDDMMYYYY(String separador,String fechaYYYYMMDD){
        StringBuffer fecha= new StringBuffer();
        
        if(!fechaYYYYMMDD.equals("")){
            fecha.append(fechaYYYYMMDD.substring(6));
            fecha.append(separador);
            fecha.append(fechaYYYYMMDD.substring(4,6));
            fecha.append(separador);
            fecha.append(fechaYYYYMMDD.substring(0,4));
        }
        
        return fecha.toString();
    }
    
    
    public static String formatDDMMYYYY(String separador,Date date){
        if(date == null)
            date = getDate();
        
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        
        String fecha;
        
        String strDia;
        int dia = calendar.get(Calendar.DAY_OF_MONTH);
        if(dia < 10)
            strDia = "0" + dia ;
        else
            strDia = dia + "";
        fecha = strDia + separador;
        
        String strMes;
        int mes = calendar.get(Calendar.MONTH) + 1;
        if(mes < 10)
            strMes = "0" + mes ;
        else
            strMes = mes + "";
        fecha = fecha + strMes + separador;
        
        fecha = fecha + calendar.get(Calendar.YEAR);
        
        return fecha;
    }
    
    public static String[] convertTimeSSLongToString(long time) {
        String data[] = new String[2];
        
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(time));
        
        StringBuffer buffer = new StringBuffer();
        
        if (calendar.get(Calendar.DAY_OF_MONTH) < 10) {
            buffer.append(0);
        }
        buffer.append(calendar.get(Calendar.DAY_OF_MONTH));
        buffer.append("/");
        
        if ((calendar.get(Calendar.MONTH) +1) < 10) {
            buffer.append(0);
        }
        buffer.append((calendar.get(Calendar.MONTH) +1));
        buffer.append("/");
        
        buffer.append(calendar.get(Calendar.YEAR));
        
        data[0] = buffer.toString();
        
        buffer = new StringBuffer();
        
        if (calendar.get(Calendar.HOUR_OF_DAY) < 10) {
            buffer.append(0);
        }
        buffer.append(calendar.get(Calendar.HOUR_OF_DAY));
        buffer.append(":");
        
        if (calendar.get(Calendar.MINUTE) < 10) {
            buffer.append(0);
        }
        buffer.append(calendar.get(Calendar.MINUTE));
        buffer.append(":");
        
        if (calendar.get(Calendar.SECOND) < 10) {
            buffer.append(0);
        }
        buffer.append(calendar.get(Calendar.SECOND));
        
        data[1] = buffer.toString();
        
        return data;
    }
    
    public static String[] convertTimeLongToString(long time) {
        String data[] = new String[2];
        
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(time));
        
        StringBuffer buffer = new StringBuffer();
        
        if (calendar.get(Calendar.DAY_OF_MONTH) < 10) {
            buffer.append(0);
        }
        buffer.append(calendar.get(Calendar.DAY_OF_MONTH));
        buffer.append("/");
        
        if ((calendar.get(Calendar.MONTH) +1) < 10) {
            buffer.append(0);
        }
        buffer.append((calendar.get(Calendar.MONTH) +1));
        buffer.append("/");
        
        buffer.append(calendar.get(Calendar.YEAR));
        
        data[0] = buffer.toString();
        
        buffer = new StringBuffer();
        
        if (calendar.get(Calendar.HOUR_OF_DAY) < 10) {
            buffer.append(0);
        }
        buffer.append(calendar.get(Calendar.HOUR_OF_DAY));
        buffer.append(":");
        
        if (calendar.get(Calendar.MINUTE) < 10) {
            buffer.append(0);
        }
        buffer.append(calendar.get(Calendar.MINUTE));
        buffer.append(":");
        
        if (calendar.get(Calendar.SECOND) < 10) {
            buffer.append(0);
        }
        buffer.append(calendar.get(Calendar.SECOND));
        
        data[1] = buffer.toString();
        
        return data;
    }
    
    /**
     * Determines if the given month, day, and year combination is valid.
     *
     * @return true if the date is valid
     */
    public static boolean isValid( int day, int month,int year) {
        
        if ( !( month >= 1 && month <= 12 )
        || !(day >=1 && day <= 31)
        || !(year >= 1000 && year <= 9999)
        || ( day == 31 &&
                ( month == 4 /*april*/ || month == 6  /*june*/ ||
                month == 9 /*sept*/  || month == 11 /*nov*/ ) ) ||
                ( month == 2 /*feb*/ && day > 29 ) ) {
            
            return false;
        }
        
        if ( month == 2 && day == 29 ) {   // if a year is divisible by 4 it is a leap year UNLESS it is also
            // divisible by 100 AND is not divisible by 400
            if ( year % 4 > 0
                    || ( ( year % 100 == 0 ) && ( year % 400 > 0 ) ) ) {
                
                return false;
            }
        }
        
        return true;
    }
    
    public static boolean validarFecha(String fecha){
        
        boolean flag = true;
//        System.out.println("textDia.getFecha()=" + fecha);
        
        try{
            
            List lista = StringUtils.tokenize(fecha, "/");
            
            String day =  (String) lista.get(0) ;
            
            String month =  (String) lista.get(1) ;
            String year =  (String) lista.get(2);
            
//            System.out.println("day="+ day);
//            System.out.println("month="+ month);
//            System.out.println("year="+ year);
            
            if(day != null && !day.equals("") && month != null && !month.equals("") && year != null && !year.equals(""))
                flag = isValid( Integer.parseInt(day), Integer.parseInt(month), Integer.parseInt(year));
            else
                flag = false;
        }catch(Exception e){
            e.printStackTrace();
            flag = false;
        }
        
        //System.out.println("flag = " + flag);
        
        return flag;
    }
    
    //dd/mm/yyyy
    public static boolean esIgualDDMMYYYY(long fecha1, long fecha2){
        try{
            if (fecha1 == fecha2){return true;}

            if (fecha1 > 0 && fecha2 > 0){
                return convertTimeLongToString(fecha1)[0] == convertTimeLongToString(fecha2)[0];

            }else{
                return false;
            }
        }catch(Exception e){return false;}    
        
    }
    
}
